﻿Imports System
Imports System.Runtime.Serialization

Namespace System.Workflow.ComponentModel.Serialization
    Friend NotInheritable Class SimpleTypesSurrogate
        Implements ISerializationSurrogate
        ' Methods
        Private Sub System.Runtime.Serialization.ISerializationSurrogate.GetObjectData(ByVal obj As Object, ByVal info As SerializationInfo, ByVal context As StreamingContext) Implements ISerializationSurrogate.GetObjectData
            If (obj.GetType Is GetType(Guid)) Then
                Dim guid As Guid = DirectCast(obj, Guid)
                info.AddValue("typeID", TypeID.Guid)
                info.AddValue("bits", guid.ToByteArray)
            End If
        End Sub

        Private Function System.Runtime.Serialization.ISerializationSurrogate.SetObjectData(ByVal obj As Object, ByVal info As SerializationInfo, ByVal context As StreamingContext, ByVal selector As ISurrogateSelector) As Object Implements ISerializationSurrogate.SetObjectData
            Dim eid As TypeID = DirectCast(info.GetValue("typeID", GetType(TypeID)), TypeID)
            If (eid = TypeID.Guid) Then
                Return New Guid([TryCast](info.GetValue("bits", GetType(Byte())),Byte()))
            End If
            Return Nothing
        End Function


        ' Nested Types
        Private Enum TypeID As Byte
            ' Fields
            Guid = 1
            [Null] = 2
        End Enum
    End Class
End Namespace

